﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using StudentModule.Models;
using StudentModule.Services;
using ServiceAgent;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using StudentModule.Views;
using ServiceAgent.StudentService;
using System.Collections.Generic;
using ServiceAgent.CatalogueService;
using StudentModule.Events;

namespace StudentModule.ViewModels
{
    /// <summary>
    /// ViewModel for SinhVienDetailView.
    /// </summary>
    public class SinhVienDetailViewModel : NotificationObject, INavigationAware
    {
        #region Fields
        private IRegionNavigationService navigationService;

        private IEventAggregator eventAggregator;
        private readonly IUnityContainer container;
        private IRegionManager regionManager;

        int uid;
        SinhVienDetailDTO sinhVien;
        string nopStatus = "Đã nộp đủ hồ sơ";
        bool IsDu;

        KhenThuongDTO currentKhenThuong;
        KyLuatDTO currentKyLuat;
        VanBangDuocNhanDTO currentVanBangDuocNhan;
        HocBongDTO currentHocBong;
        CauLacBoThamGiaDTO currentCauLacBoThamGia;
        HoatDongDoanTheThamGiaDTO currentHoatDongDoanTheThamGia;
        ThayDoiTrongQuaTrinhHocDTO currentThayDoiTrongQuaTrinhHoc;
        NghienCuuKhoaHocDTO currentNghienCuuKhoaHoc;
        ThamGiaOlympicDTO currentThamGiaOlympic;

        ObservableCollection<string> xepLoaiHocLucList;
        ObservableCollection<string> xepLoaiTotNghiepList;

        ObservableCollection<LoaiThayDoiDTO> loaiThayDoiList;
        ObservableCollection<DonViDTO> donViList;
        ObservableCollection<LopDTO> lopList;
        ObservableCollection<DanTocDTO> danTocList;
        ObservableCollection<TonGiaoDTO> tonGiaoList;
        ObservableCollection<ServiceAgent.CatalogueService.ChinhSachDTO> chinhSachList;
        ObservableCollection<DVHCCap1DTO> dvhcCap1List;
        ObservableCollection<DVHCCap2DTO> dvhcCap2ThuongTruList;
        ObservableCollection<DVHCCap2DTO> dvhcCap2TamTruList;

        DonViDTO currentDonVi;
        DVHCCap1DTO thuongTru;
        DVHCCap1DTO tamTru;

        public InteractionRequest<Notification> ViewHoSoDaNopRequest { get; private set; }

        public InteractionRequest<Notification> AddNewHocBongRequest { get; private set; }
        public InteractionRequest<Notification> AddNewKyLuatRequest { get; private set; }
        public InteractionRequest<Notification> AddNewKhenThuongRequest { get; private set; }
        public InteractionRequest<Notification> AddNewCauLacBoRequest { get; private set; }
        public InteractionRequest<Notification> AddNewHoatDongDoanTheRequest { get; private set; }
        public InteractionRequest<Notification> AddNewNghienCuuKhoaHocRequest { get; private set; }
        public InteractionRequest<Notification> AddNewThamGiaOlympicRequest { get; private set; }
        public InteractionRequest<Notification> AddNewThayDoiRequest { get; private set; }
        public InteractionRequest<Notification> AddNewVanBangRequest { get; private set; }
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public SinhVienDetailViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.container = container;
        }
        #endregion

        #region Administrative Properties
        public ObservableCollection<LoaiThayDoiDTO> LoaiThayDoiList
        {
            get { return loaiThayDoiList; }
            set { loaiThayDoiList = value;
            RaisePropertyChanged(() => this.LoaiThayDoiList);
            }
        }

        public ObservableCollection<string> XepLoaiHocLucList
        {
            get { return xepLoaiHocLucList; }
            set
            {
                xepLoaiHocLucList = value;
                RaisePropertyChanged(() => this.XepLoaiHocLucList);
            }
        }

        public ObservableCollection<string> XepLoaiTotNghiepList
        {
            get { return xepLoaiTotNghiepList; }
            set
            {
                xepLoaiTotNghiepList = value;
                RaisePropertyChanged(() => this.XepLoaiTotNghiepList);
            }
        }

        public ServiceAgent.StudentService.SinhVienDetailDTO SinhVien
        {
            get { return sinhVien; }
            set
            {
                sinhVien = value;
                RaisePropertyChanged(() => this.SinhVien);
            }
        }

        public string NopStatus
        {
            get { return nopStatus; }
            set
            {
                nopStatus = value;
                RaisePropertyChanged(() => this.NopStatus);
            }
        }

        public ServiceAgent.CatalogueService.DonViDTO CurrentDonVi
        {
            get { return currentDonVi; }
            set
            {
                currentDonVi = value;
                RaisePropertyChanged(() => this.CurrentDonVi);
                this.GetLopListByDonViChange();
            }
        }

        public ServiceAgent.CatalogueService.DVHCCap1DTO ThuongTru
        {
            get { return thuongTru; }
            set
            {
                thuongTru = value;
                RaisePropertyChanged(() => this.ThuongTru);
                GetDVHCCap2ThuongTruByDVHCCap1Change();
            }
        }

        public ServiceAgent.CatalogueService.DVHCCap1DTO TamTru
        {
            get { return tamTru; }
            set
            {
                tamTru = value;
                RaisePropertyChanged(() => this.TamTru);
                GetDVHCCap2TamTruByDVHCCap1Change();
            }
        }

        public ObservableCollection<DonViDTO> DonViList
        {
            get { return donViList; }
            set
            {
                donViList = value;
                RaisePropertyChanged(() => this.DonViList);
            }
        }

        public ObservableCollection<LopDTO> LopList
        {
            get { return lopList; }
            set
            {
                lopList = value;
                RaisePropertyChanged(() => this.LopList);
            }
        }

        public ObservableCollection<DanTocDTO> DanTocList
        {
            get { return danTocList; }
            set
            {
                danTocList = value;
                RaisePropertyChanged(() => this.DanTocList);
            }
        }

        public ObservableCollection<TonGiaoDTO> TonGiaoList
        {
            get { return tonGiaoList; }
            set
            {
                tonGiaoList = value;
                RaisePropertyChanged(() => this.TonGiaoList);
            }
        }

        public ObservableCollection<ServiceAgent.CatalogueService.ChinhSachDTO> ChinhSachList
        {
            get { return chinhSachList; }
            set
            {
                chinhSachList = value;
                RaisePropertyChanged(() => this.ChinhSachList);
            }
        }

        public ObservableCollection<DVHCCap1DTO> DVHCCap1List
        {
            get { return dvhcCap1List; }
            set
            {
                dvhcCap1List = value;
                RaisePropertyChanged(() => this.DVHCCap1List);
            }
        }

        public ObservableCollection<DVHCCap2DTO> DVHCCap2ThuongTruList
        {
            get { return dvhcCap2ThuongTruList; }
            set
            {
                dvhcCap2ThuongTruList = value;
                RaisePropertyChanged(() => this.DVHCCap2ThuongTruList);
            }
        }

        public ObservableCollection<DVHCCap2DTO> DVHCCap2TamTruList
        {
            get { return dvhcCap2TamTruList; }
            set
            {
                dvhcCap2TamTruList = value;
                RaisePropertyChanged(() => this.DVHCCap2TamTruList);
            }
        }

        public ServiceAgent.StudentService.KyLuatDTO CurrentKyLuat
        {
            get { return currentKyLuat; }
            set
            {
                currentKyLuat = value;
                RaisePropertyChanged(() => this.CurrentKyLuat);
                this.DeleteKyLuatCommand.RaiseCanExecuteChanged();
            }
        }
        public ServiceAgent.StudentService.KhenThuongDTO CurrentKhenThuong
        {
            get { return currentKhenThuong; }
            set
            {
                currentKhenThuong = value;
                RaisePropertyChanged(() => this.CurrentKhenThuong);
                this.DeleteKhenThuongCommand.RaiseCanExecuteChanged();
            }
        }
        public ServiceAgent.StudentService.VanBangDuocNhanDTO CurrentVanBangDuocNhan
        {
            get { return currentVanBangDuocNhan; }
            set
            {
                currentVanBangDuocNhan = value;
                RaisePropertyChanged(() => this.CurrentVanBangDuocNhan);
                this.DeleteVanBangCommand.RaiseCanExecuteChanged();
            }
        }
        public ServiceAgent.StudentService.HocBongDTO CurrentHocBong
        {
            get { return currentHocBong; }
            set
            {
                currentHocBong = value;
                RaisePropertyChanged(() => this.CurrentHocBong);
                this.DeleteHocBongCommand.RaiseCanExecuteChanged();
            }
        }
        public ServiceAgent.StudentService.CauLacBoThamGiaDTO CurrentCauLacBoThamGia
        {
            get { return currentCauLacBoThamGia; }
            set
            {
                currentCauLacBoThamGia = value;
                RaisePropertyChanged(() => this.CurrentCauLacBoThamGia);
                this.DeleteCauLacBoCommand.RaiseCanExecuteChanged();
            }
        }
        public ServiceAgent.StudentService.HoatDongDoanTheThamGiaDTO CurrentHoatDongDoanTheThamGia
        {
            get { return currentHoatDongDoanTheThamGia; }
            set
            {
                currentHoatDongDoanTheThamGia = value;
                RaisePropertyChanged(() => this.CurrentHoatDongDoanTheThamGia);
                this.DeleteHoatDongDoanTheCommand.RaiseCanExecuteChanged();
            }
        }
        public ServiceAgent.StudentService.ThayDoiTrongQuaTrinhHocDTO CurrentThayDoiTrongQuaTrinhHoc
        {
            get { return currentThayDoiTrongQuaTrinhHoc; }
            set
            {
                currentThayDoiTrongQuaTrinhHoc = value;
                RaisePropertyChanged(() => this.CurrentThayDoiTrongQuaTrinhHoc);
                this.DeleteThayDoiCommand.RaiseCanExecuteChanged();
            }
        }
        public ServiceAgent.StudentService.NghienCuuKhoaHocDTO CurrentNghienCuuKhoaHoc
        {
            get { return currentNghienCuuKhoaHoc; }
            set
            {
                currentNghienCuuKhoaHoc = value;
                RaisePropertyChanged(() => this.CurrentNghienCuuKhoaHoc);
                this.DeleteNghienCuuKhoaHocCommand.RaiseCanExecuteChanged();
            }
        }
        public ServiceAgent.StudentService.ThamGiaOlympicDTO CurrentThamGiaOlympic
        {
            get { return currentThamGiaOlympic; }
            set
            {
                currentThamGiaOlympic = value;
                RaisePropertyChanged(() => this.CurrentThamGiaOlympic);
                this.DeleteThamGiaOlympicCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion

        #region Command Properties
        public ICommand SaveCommand { get; private set; }
        public DelegateCommand<object> GoBackCommand { get; private set; }

        public ICommand HoSoDaNopCommand { get; private set; }

        public ICommand AddNewKhenThuongCommand { get; private set; }
        public ICommand SaveKhenThuongCommand { get; private set; }
        public DelegateCommand DeleteKhenThuongCommand { get; private set; }

        public ICommand AddNewKyLuatCommand { get; private set; }
        public ICommand SaveKyLuatCommand { get; private set; }
        public DelegateCommand DeleteKyLuatCommand { get; private set; }

        public ICommand AddNewHocBongCommand { get; private set; }
        public ICommand SaveHocBongCommand { get; private set; }
        public DelegateCommand DeleteHocBongCommand { get; private set; }

        public ICommand AddNewCauLacBoCommand { get; private set; }
        public ICommand SaveCauLacBoCommand { get; private set; }
        public DelegateCommand DeleteCauLacBoCommand { get; private set; }

        public ICommand AddNewNghienCuuKhoaHocCommand { get; private set; }
        public ICommand SaveNghienCuuKhoaHocCommand { get; private set; }
        public DelegateCommand DeleteNghienCuuKhoaHocCommand { get; private set; }

        public ICommand AddNewThamGiaOlympicCommand { get; private set; }
        public ICommand SaveThamGiaOlympicCommand { get; private set; }
        public DelegateCommand DeleteThamGiaOlympicCommand { get; private set; }

        public ICommand AddNewHoatDongDoanTheCommand { get; private set; }
        public ICommand SaveHoatDongDoanTheCommand { get; private set; }
        public DelegateCommand DeleteHoatDongDoanTheCommand { get; private set; }

        public ICommand AddNewVanBangCommand { get; private set; }
        public ICommand SaveVanBangCommand { get; private set; }
        public DelegateCommand DeleteVanBangCommand { get; private set; }

        public ICommand AddNewThayDoiCommand { get; private set; }
        public ICommand SaveThayDoiCommand { get; private set; }
        public DelegateCommand DeleteThayDoiCommand { get; private set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {
            this.XepLoaiHocLucList = Lists.HocLucList;
            this.XepLoaiTotNghiepList = Lists.TotNghiepList;

            this.SaveCommand = new DelegateCommand(this.OnRaiseSave, CanSave);
            this.GoBackCommand = new DelegateCommand<object>(GoBack, CanGoBack);

            this.HoSoDaNopCommand = new DelegateCommand(this.OnViewHoSoDaNopDetail);
            this.ViewHoSoDaNopRequest = new InteractionRequest<Notification>();

            this.AddNewKhenThuongCommand = new DelegateCommand(this.OnRaiseAddNewKhenThuong);
            this.AddNewKhenThuongRequest = new InteractionRequest<Notification>();
            this.SaveKhenThuongCommand = new DelegateCommand(this.OnRaiseSaveKhenThuong);
            this.DeleteKhenThuongCommand = new DelegateCommand(this.OnRaiseDeleteKhenThuong, this.CanRaiseDeleteKhenThuong);

            this.AddNewKyLuatCommand = new DelegateCommand(this.OnRaiseAddNewKyLuat);
            this.AddNewKyLuatRequest = new InteractionRequest<Notification>();
            this.SaveKyLuatCommand = new DelegateCommand(this.OnRaiseSaveKyLuat);
            this.DeleteKyLuatCommand = new DelegateCommand(this.OnRaiseDeleteKyLuat, this.CanRaiseDeleteKyLuat);

            this.AddNewHocBongCommand = new DelegateCommand(this.OnRaiseAddNewHocBong);
            this.AddNewHocBongRequest = new InteractionRequest<Notification>();
            this.SaveHocBongCommand = new DelegateCommand(this.OnRaiseSaveHocBong);
            this.DeleteHocBongCommand = new DelegateCommand(this.OnRaiseDeleteHocBong, this.CanRaiseDeleteHocBong);

            this.AddNewCauLacBoCommand = new DelegateCommand(this.OnRaiseAddNewCauLacBo);
            this.AddNewCauLacBoRequest = new InteractionRequest<Notification>();
            this.SaveCauLacBoCommand = new DelegateCommand(this.OnRaiseSaveCauLacBo);
            this.DeleteCauLacBoCommand = new DelegateCommand(this.OnRaiseDeleteCauLacBo, this.CanRaiseDeleteCauLacBo);

            this.AddNewNghienCuuKhoaHocCommand = new DelegateCommand(this.OnRaiseAddNewNghienCuuKhoaHoc);
            this.AddNewNghienCuuKhoaHocRequest = new InteractionRequest<Notification>();
            this.SaveNghienCuuKhoaHocCommand = new DelegateCommand(this.OnRaiseSaveNghienCuuKhoaHoc);
            this.DeleteNghienCuuKhoaHocCommand = new DelegateCommand(this.OnRaiseDeleteNghienCuuKhoaHoc, this.CanRaiseDeleteNghienCuuKhoaHoc);

            this.AddNewThamGiaOlympicCommand = new DelegateCommand(this.OnRaiseAddNewThamGiaOlympic);
            this.AddNewThamGiaOlympicRequest = new InteractionRequest<Notification>();
            this.SaveThamGiaOlympicCommand = new DelegateCommand(this.OnRaiseSaveThamGiaOlympic);
            this.DeleteThamGiaOlympicCommand = new DelegateCommand(this.OnRaiseDeleteThamGiaOlympic, this.CanRaiseDeleteThamGiaOlympic);

            this.AddNewHoatDongDoanTheCommand = new DelegateCommand(this.OnRaiseAddNewHoatDongDoanThe);
            this.AddNewHoatDongDoanTheRequest = new InteractionRequest<Notification>();
            this.SaveHoatDongDoanTheCommand = new DelegateCommand(this.OnRaiseSaveHoatDongDoanThe);
            this.DeleteHoatDongDoanTheCommand = new DelegateCommand(this.OnRaiseDeleteHoatDongDoanThe, this.CanRaiseDeleteHoatDongDoanThe);

            this.AddNewVanBangCommand = new DelegateCommand(this.OnRaiseAddNewVanBang);
            this.AddNewVanBangRequest = new InteractionRequest<Notification>();
            this.SaveVanBangCommand = new DelegateCommand(this.OnRaiseSaveVanBang);
            this.DeleteVanBangCommand = new DelegateCommand(this.OnRaiseDeleteVanBang, this.CanRaiseDeleteVanBang);

            this.AddNewThayDoiCommand = new DelegateCommand(this.OnRaiseAddNewThayDoi);
            this.AddNewThayDoiRequest = new InteractionRequest<Notification>();
            this.SaveThayDoiCommand = new DelegateCommand(this.OnRaiseSaveThayDoi);
            this.DeleteThayDoiCommand = new DelegateCommand(this.OnRaiseDeleteThayDoi, this.CanRaiseDeleteThayDoi);
        }

        #region SinhVien Info Methods
        void GetSinhVien(int id)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien = proxy.GetSinhVienDetailByID(id);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
        }

        void GetDanhSachCauLacBo(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachCauLacBo = proxy.GetCauLacBoThamGiaBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachHocBong(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachHocBong = proxy.GetHocBongBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachKhenThuong(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachKhenThuong = proxy.GetKhenThuongBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachKyLuat(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachKyLuat = proxy.GetKyLuatBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachHoatDongDoanThe(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachHoatDongDoanThe = proxy.GetHoatDongDoanTheThamGiaBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachNghienCuuKhoaHoc(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachNghienCuuKhoaHoc = proxy.GetNghienCuuKhoaHocBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachNhatKy(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachNhatKy = proxy.GetNhatKyBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachThamGiaOlympic(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachThamGiaOlympic = proxy.GetThamGiaOlympicBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachThayDoi(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachThayDoi = proxy.GetThayDoiTrongQuaTrinhHocBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanhSachVanBang(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachVanBang = proxy.GetVanBangDuocNhanBySinhVienID(SinhVienID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetHoSoDaNop(int SinhVienID)
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.DanhSachHoSoDaNop = proxy.GetHoSoDaNopBySinhVienID(SinhVienID);
                }
                this.CheckNopStatus();
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        #endregion

        #region Addnew Methods
        private void OnRaiseAddNewKhenThuong()
        {
            this.eventAggregator.GetEvent<AddKhenThuongEvent>().Publish(SinhVien.ID);
            this.AddNewKhenThuongRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachKhenThuong(SinhVien.ID); });
        }

        private void OnRaiseAddNewKyLuat()
        {
            this.eventAggregator.GetEvent<AddKyLuatEvent>().Publish(SinhVien.ID);
            this.AddNewKyLuatRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachKyLuat(SinhVien.ID); });
        }

        private void OnRaiseAddNewHocBong()
        {
            this.eventAggregator.GetEvent<AddHocBongEvent>().Publish(SinhVien.ID);
            this.AddNewHocBongRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachHocBong(SinhVien.ID); });
        }

        private void OnRaiseAddNewCauLacBo()
        {
            this.eventAggregator.GetEvent<AddCauLacBoEvent>().Publish(SinhVien.ID);
            this.AddNewCauLacBoRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachCauLacBo(SinhVien.ID); });
        }

        private void OnRaiseAddNewNghienCuuKhoaHoc()
        {
            this.eventAggregator.GetEvent<AddNghienCuuKhoaHocEvent>().Publish(SinhVien.ID);
            this.AddNewNghienCuuKhoaHocRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachNghienCuuKhoaHoc(SinhVien.ID); });
        }

        private void OnRaiseAddNewThamGiaOlympic()
        {
            this.eventAggregator.GetEvent<AddThamGiaOlympicEvent>().Publish(SinhVien.ID);
            this.AddNewThamGiaOlympicRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachThamGiaOlympic(SinhVien.ID); });
        }

        private void OnRaiseAddNewHoatDongDoanThe()
        {
            this.eventAggregator.GetEvent<AddHoatDongDoanTheEvent>().Publish(SinhVien.ID);
            this.AddNewHoatDongDoanTheRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachHoatDongDoanThe(SinhVien.ID); });
        }

        private void OnRaiseAddNewVanBang()
        {
            this.eventAggregator.GetEvent<AddVanBangEvent>().Publish(SinhVien.ID);
            this.AddNewVanBangRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachVanBang(SinhVien.ID); });
        }

        private void OnRaiseAddNewThayDoi()
        {
            this.eventAggregator.GetEvent<AddThayDoiTrongQuaTrinhHocTapEvent>().Publish(SinhVien.ID);
            this.AddNewThayDoiRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetDanhSachThayDoi(SinhVien.ID); });
        }
        #endregion

        #region Save Methods
        private void OnRaiseSaveKhenThuong()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveKhenThuongList(SinhVien.DanhSachKhenThuong);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSaveKyLuat()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveKyLuatList(SinhVien.DanhSachKyLuat);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSaveHocBong()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveHocBongList(SinhVien.DanhSachHocBong);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSaveCauLacBo()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveCauLacBoThamGiaList(SinhVien.DanhSachCauLacBo);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSaveNghienCuuKhoaHoc()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveNghienCuuKhoaHocList(SinhVien.DanhSachNghienCuuKhoaHoc);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSaveThamGiaOlympic()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveThamGiaOlympicList(SinhVien.DanhSachThamGiaOlympic);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSaveHoatDongDoanThe()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveHoatDongDoanTheThamGiaList(SinhVien.DanhSachHoatDongDoanThe);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSaveVanBang()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveVanBangDuocNhanList(SinhVien.DanhSachVanBang);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSaveThayDoi()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.SaveThayDoiTrongQuaTrinhHocList(SinhVien.DanhSachThayDoi);
                    RaisePropertyChanged(() => this.SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        #endregion

        #region Delete Methods
        private void OnRaiseDeleteKyLuat()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveKyLuat(CurrentKyLuat);
                    SinhVien.DanhSachKyLuat.Remove(CurrentKyLuat);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteKyLuat()
        {
            return CurrentKyLuat != null;
        }

        private void OnRaiseDeleteKhenThuong()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveKhenThuong(CurrentKhenThuong);
                    SinhVien.DanhSachKhenThuong.Remove(CurrentKhenThuong);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteKhenThuong()
        {
            return CurrentKhenThuong != null;
        }

        private void OnRaiseDeleteVanBang()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveVanBangDuocNhan(CurrentVanBangDuocNhan);
                    SinhVien.DanhSachVanBang.Remove(CurrentVanBangDuocNhan);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteVanBang()
        {
            return CurrentVanBangDuocNhan != null;
        }

        private void OnRaiseDeleteHocBong()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveHocBong(CurrentHocBong);
                    SinhVien.DanhSachHocBong.Remove(CurrentHocBong);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteHocBong()
        {
            return CurrentHocBong != null;
        }

        private void OnRaiseDeleteCauLacBo()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveCauLacBoThamGia(CurrentCauLacBoThamGia);
                    SinhVien.DanhSachCauLacBo.Remove(CurrentCauLacBoThamGia);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteCauLacBo()
        {
            return CurrentCauLacBoThamGia != null;
        }

        private void OnRaiseDeleteHoatDongDoanThe()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveHoatDongDoanTheThamGia(CurrentHoatDongDoanTheThamGia.ID);
                    SinhVien.DanhSachHoatDongDoanThe.Remove(CurrentHoatDongDoanTheThamGia);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteHoatDongDoanThe()
        {
            return CurrentHoatDongDoanTheThamGia != null;
        }

        private void OnRaiseDeleteThayDoi()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveThayDoiTrongQuaTrinhHoc(CurrentThayDoiTrongQuaTrinhHoc);
                    SinhVien.DanhSachThayDoi.Remove(CurrentThayDoiTrongQuaTrinhHoc);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteThayDoi()
        {
            return CurrentThayDoiTrongQuaTrinhHoc != null;
        }

        private void OnRaiseDeleteNghienCuuKhoaHoc()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveNghienCuuKhoaHoc(CurrentNghienCuuKhoaHoc);
                    SinhVien.DanhSachNghienCuuKhoaHoc.Remove(CurrentNghienCuuKhoaHoc);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteNghienCuuKhoaHoc()
        {
            return CurrentNghienCuuKhoaHoc != null;
        }

        private void OnRaiseDeleteThamGiaOlympic()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    proxy.RemoveThamGiaOlympic(CurrentThamGiaOlympic);
                    SinhVien.DanhSachThamGiaOlympic.Remove(CurrentThamGiaOlympic);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }
        private bool CanRaiseDeleteThamGiaOlympic()
        {
            return CurrentThamGiaOlympic != null;
        }
        #endregion

        void CheckNopStatus()
        {
            IsDu = true;
            if (SinhVien.DanhSachHoSoDaNop.Count > 0)
            {
                foreach (var hoso in SinhVien.DanhSachHoSoDaNop)
                {
                    if (hoso.IsTrue == false)
                    {
                        IsDu = false;
                    }
                }
                if (IsDu)
                {
                    NopStatus = "Đã nộp đủ hồ sơ";
                }
                else
                {
                    NopStatus = "Chưa nộp đủ hồ sơ";
                }
            }
        }

        void GetLoaiThayDoiList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    LoaiThayDoiList = proxy.GetLoaiThayDoiList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDonViList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DonViList = proxy.GetDonViListByCapDonVi();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetLopListByDonVi()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    LopList = proxy.GetLopByDonViID(SinhVien.DonViID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetLopListByDonViChange()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    LopList = proxy.GetLopByDonViID(CurrentDonVi.ID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap1List()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap1List = proxy.GetDVHCCap1List();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap2ThuongTruByDVHCCap1()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap2ThuongTruList = proxy.GetDVHCCap2ListByDVHCCap1ID(SinhVien.LienLac.DiaChiThuongTru.TinhThanhID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap2ThuongTruByDVHCCap1Change()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap2ThuongTruList = proxy.GetDVHCCap2ListByDVHCCap1ID(ThuongTru.ID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap2TamTruByDVHCCap1()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap2TamTruList = proxy.GetDVHCCap2ListByDVHCCap1ID(SinhVien.LienLac.DiaChiThuongTru.TinhThanhID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDVHCCap2TamTruByDVHCCap1Change()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DVHCCap2TamTruList = proxy.GetDVHCCap2ListByDVHCCap1ID(TamTru.ID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetTonGiaoList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    TonGiaoList = proxy.GetTonGiaoList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetDanTocList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DanTocList = proxy.GetDanTocList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetChinhSachList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    ChinhSachList = proxy.GetChinhSachList();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSave()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    SinhVien.HinhAnh = null;
                    proxy.SaveSinhVien(SinhVien);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception) { MessageBox.Show("Có lỗi xảy ra"); }
        }

        private bool CanSave()
        {
            return PermissionChecker.HasQuyenChinhSuaThongTinSinhVien();
        }

        private void GoBack(object commandArg)
        {
            if (navigationService.Journal.CanGoBack)
            {
                navigationService.Journal.GoBack();
            }
        }

        private bool CanGoBack(object commandArg)
        {
            return navigationService.Journal.CanGoBack;
        }

        private void OnViewHoSoDaNopDetail()
        {
            this.eventAggregator.GetEvent<NopHoSoEvent>().Publish(SinhVien.ID);
            this.eventAggregator.GetEvent<SendHoSoDaNopEvent>().Publish(SinhVien.DanhSachHoSoDaNop);
            this.ViewHoSoDaNopRequest.Raise(
               new Notification { Title = "Chi tiết hồ sơ đã nộp" },
               dummy => { this.GetHoSoDaNop(SinhVien.ID); });
        }
        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            // getting a reference to navigation service:
            if (navigationService == null)
                navigationService = navigationContext.NavigationService;

            //// Retrieve any required paramaters from the navigation Uri.
            string id = navigationContext.Parameters["ID"];
            if (!string.IsNullOrEmpty(id))
            {
                this.uid = Convert.ToInt32(id);
                try
                {
                    this.GetSinhVien(uid);
                    this.GetChinhSachList();
                    this.GetDanTocList();
                    this.GetTonGiaoList();
                    this.GetDonViList();
                    this.GetLopListByDonVi();
                    this.GetDVHCCap1List();
                    this.GetDVHCCap2TamTruByDVHCCap1();
                    this.GetDVHCCap2ThuongTruByDVHCCap1();
                    this.GetLoaiThayDoiList();
                    this.GetDanhSachCauLacBo(SinhVien.ID);
                    this.GetDanhSachHoatDongDoanThe(SinhVien.ID);
                    this.GetDanhSachHocBong(SinhVien.ID);
                    //this.GetDanhHoSoDaNop(SinhVien.ID);
                    this.GetDanhSachKhenThuong(SinhVien.ID);
                    this.GetDanhSachKyLuat(SinhVien.ID);
                    this.GetDanhSachNghienCuuKhoaHoc(SinhVien.ID);
                    this.GetDanhSachNhatKy(SinhVien.ID);
                    this.GetDanhSachThamGiaOlympic(SinhVien.ID);
                    this.GetDanhSachThayDoi(SinhVien.ID);
                    this.GetDanhSachVanBang(SinhVien.ID);
                    if (SinhVien.LopID != null)
                    {
                        this.GetHoSoDaNop(SinhVien.ID);
                    }
                }
                catch
                {
                    MessageBox.Show("Có lỗi xảy ra");
                }

                this.Initialize();
            }
        }
        #endregion
    }
}
